<?php defined('SYSPATH') or die('No direct script access.');
	
	class Controller_Patient extends Controller{
						
		public function action_index(){
			$grid = new Jqgrid("patient", Helper_Service::factory("patient"));
			$view = View::factory("patients/patient-list");
			$view->patient_list = $grid->render("jqgrid/config/patient");
						
			$this->request->response = $view->render();
		}
		
		public function action_list(){
			$grid = new Jqgrid("patient", Helper_Service::factory("patient"));
			
			$criteria = new Jqgrid_Criteria();
			$criteria->page = $_POST['page'];
			$criteria->limit = $_POST['rows'];
			$criteria->sidx = $_POST['sidx'];
			$criteria->sord = $_POST['sord'];
			
			if (isset($_POST['_search'])){
				if (($search = $_POST['_search']) == "true"){
					$criteria->isSearch = true;
					$criteria->searchField = $_POST['searchField'];
					$criteria->searchOper = $_POST['searchOper'];
		 			$criteria->searchString = $_POST['searchString'];
				}
			}
			
			$this->request->response = $grid->getList($criteria);
		}
		
		public function action_new(){
			$this->request->response = $this->patientForm()->render();
		}
		
		public function action_get(){
			$id = $_POST['id'];
			$this->request->response = $this->patientForm($id)->render();	
		}
		
		public function patientForm($id = ""){
			$tab_actions = array();
			$tab_actions[] = array("url"=>"/diastole/patient/personalinfo/$id","text"=>"Personales");
			$tab_actions[] = array("url"=>"/diastole/patient/monitoring/$id","text"=>"Seguimiento");
			$tab_actions[] = array("url"=>"/diastole/patient/habits/$id","text"=>"H&aacute;bitos");
			$tab_actions[] = array("url"=>"/diastole/patient/pathologies/$id","text"=>"Patolog&iacute;as");
			$tab_actions[] = array("url"=>"/diastole/patient/surgeries/$id","text"=>"Cirug&iacute;as");
			$tab_actions[] = array("url"=>"/diastole/patient/medicines/$id","text"=>"Medicamentos");
			$tab_actions[] = array("url"=>"/diastole/patient/familyhistory/$id","text"=>"Antecedentes");
			
			//Tabs creation
			$patient_tabs = View::factory("components/tabs");
			$patient_tabs->actions = $tab_actions;
									
			$patient_view = View::factory("patients/patient-form");
			$patient_view->actions = $patient_tabs->render();
			$patient_view->id_patient = $id;
			$patient_view->fullname = ($id) ? Helper_Service::factory("patient",$id)->getFullname() : "";
			 
			return $patient_view;
		}
		
		
		public function action_personalInfo(){
						
			$id_patient = $this->request->param("id"); //falta validar el id
			
			if ($id_patient){
				$patient = Helper_Service::factory("patient",$id_patient); 	
			}else{
				$patient = Helper_Service::factory("patient");
				$patient->newPatient();
			}
						
			$view = View::factory("patients/personal-info");
			$view->id_patient = $id_patient;
			$view->patient = $patient;
			$view->doctypes = Helper_Service::factory("identitycard")->getAll();
			$view->professions = Helper_Service::factory("profession")->getAll();
						
			$this->request->response = $view->render();
		}
			
		public function action_savePersonalInfo(){
			$id_patient = ($_POST['id'])? intval($_POST['id']): 0;
			$service =  Helper_Service::factory("patient");
			$service->save($id_patient,$_POST);
			$this->request->response = $service->patient->id;
		}
									
		public function action_habits(){
			$id_patient = $this->request->param("id"); //falta validar el id
			$patient = Helper_Service::factory("patient",$id_patient);
			$emotionLevels = array("Alto","Medio","Bajo");
			
			if ($patient->getHabits() != NULL){
				$patient_habit = $patient->getHabits(); 
			}else{
				$patient_habit = Helper_Service::factory("habit");
				$patient_habit->newHabit($id_patient);  
			}
			
			$habit = array();
			$habit["patient_id"] = $id_patient;
			$habit["id"] = $patient_habit->getId(); 
			$habit["angry_level"] = $patient_habit->getAngryLevel();
			$habit["stress_level"] = $patient_habit->getStressLevel();
			$habit["sedentary"] = $patient_habit->isSedentary();
			$habit["blocks_per_day"] = $patient_habit->blocksPerDay();
			$habit["workplace"] = $patient_habit->workplace();
			$habit["consume_alcohol"] = $patient_habit->consumesAlcohol();
			$habit["automedication"] = $patient_habit->autoMedicates();
		 	$habit["cigarettes_per_day"] = $patient_habit->cigarettesPerDay();
		  	$habit["smoke_start"] = $patient_habit->startAgeSmoking();
		  	$habit["smoke_end"] = $patient_habit->endAgeSmoking();
			$habit["sedentary_check"] = ($patient_habit->isSedentary()) ? 'checked="checked"' : "";
		 	$habit["consume_alcohol_check"] = ($patient_habit->consumesAlcohol())? 'checked="checked"' :"";
		 	$habit["automedication_check"] = ($patient_habit->autoMedicates())? 'checked="checked"' : "";
		 	$habit["not_smoked_check"] = ($patient_habit->neverSmoked()) ? 'checked="checked"' : "" ;
		 	$habit["has_smoked_check"] = ($patient_habit->hasQuitSmoking()) ? 'checked="checked"' : "" ;
		 	$habit["still_smokes_check"] = ($patient_habit->currentlySmoking()) ? 'checked="checked"' : "" ; 
		  							
			$view = View::factory("patients/habits");
			$view->habit = $habit; 
			$view->angryLevels = $emotionLevels;
			$view->stressLevels = $emotionLevels;
			$view->activities_content = Request::factory("activitypatient/index/$id_patient")->execute()->response;
									
			$this->request->response = $view->render();
		}
			
		public function action_saveHabits(){
			$id_patient = $this->request->param("id");
			$id_habit = ($_POST['id'])? intval($_POST['id']): 0;
			
			switch($_POST["smoke"]){
				case 1:
					$smoke = 0;
					$smoked = 0;
					break;
				case 2:
					$smoke = 0;
					$smoked = 1;
					break;
				case 3:
					$smoke = 1;
					$smoked = 0;
					break;
				default:
					$smoke = 0;
					$smoked = 0;
					break;
			}
			
			$_POST["patient_id"] = $id_patient;
			$_POST["smoke"] = $smoke;
			$_POST["smoked"] = $smoked;
			
			$service =  Helper_Service::factory("habit");
			$service->save($id_habit,$_POST);
			$this->request->response = $service->habit_patient->id;
					
		}

		public function action_monitoring(){
			$id_patient = $this->request->param("id");
			$this->request->response = Request::factory("clinicalinformation/index/$id_patient")->execute()->response;
		}
		
		public function action_pathologies(){
			$id_patient = $this->request->param("id");
			$this->request->response = Request::factory("pathologypatient/index/$id_patient")->execute()->response;
		}
				
		public function action_surgeries(){
			$id_patient = $this->request->param("id");
			$this->request->response = Request::factory("surgerypatient/index/$id_patient")->execute()->response;
		}
				
		public function action_medicines(){
			$id_patient = $this->request->param("id");
			$this->request->response = Request::factory("medicinepatient/index/$id_patient")->execute()->response;
		}
				
		public function action_familyHistory(){
			$id_patient = $this->request->param("id");
			$this->request->response = Request::factory("familybackground/index/$id_patient")->execute()->response;
		}
	
		
		
	} 
// End Patient?>

